package chaoyue.study.service;

import chaoyue.study.entity.TestEntity;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    /**
     * 查询使用缓存
     */
    @Cacheable(value = "testCache_queryValueById", key = "'id euqals ' + #id")
    public TestEntity queryValueById(Integer id) {
        System.out.println("非缓存查询操作 ID：" + id);
        TestEntity testEntity = new TestEntity();
        testEntity.setId(id);
        testEntity.setValue("value is " + id);
        return testEntity;
    }

    /**
     * 插入缓存
     */
    @CachePut(value = "testCache", key = "#data.id")
    public void addValue(TestEntity data) {
        System.out.println("插入: " + data);
    }

    /**
     * 删除缓存
     */
    @CacheEvict(value = "testCache", key = "#id")
    public void deleteValue(Integer id) {
        System.out.println("删除 ID：" + id);
    }

}
